(function() {
    angular.module('app').component('darenTieProductComponent', {
        templateUrl: 'app/controllers/redNet.controller/daren.tie.product.component/daren.tie.product.component.html',
        controller: darenTieProductComponent,
        controllerAs: 'vm'
    });

    //productBasicService来自services->product.basic.service
    darenTieProductComponent.$inject = ['darenTieProductComponentService', '$loading', '$rootScope', '$scope', 'productBasicService'];

    function darenTieProductComponent(darenTieProductComponentService, $loading, $rootScope, $scope, productBasicService) {

        var vm = this;

        vm.filter = {

            sortColumn: 'sell_moeny',
            darenUid: '',
            feedId: '',
            sort: 'down',
        }

        vm.daren = {};

        vm.feed = {};

        vm.table={
            products : []
        }

        //获取日监控
        vm.getMonitorData = productBasicService.getMonitorData; //function


        var clearFun = $rootScope.$on('redNet_daren_tie_product_EV', function(e, tie, daren) {

            vm.daren = daren;

            vm.feed = tie;

            vm.filter.darenUid = daren.darenUid;

            vm.filter.feedId = tie.feedId;

            _init();
        });


        function _init() {

            $loading.start('darenTieProduct');

            var json = angular.copy(vm.filter);

            darenTieProductComponentService.getDarenTieProduct(json).then(function(data) {

                productBasicService.monitorDataInit();

                vm.table.products = data;

                angular.forEach(vm.table.products, function(prod, index) {

                    if (prod.sellAmount == null ||prod.sellAmount == 0) {

                        prod.avgPrice = 0;

                    } else {
                        prod.avgPrice = (prod.sellMoney / prod.sellAmount).toFixed(2);
                    }


                });

                $loading.finish('darenTieProduct');
            }, function(err) {});
        }


        $scope.$on('$destroy',function(){
            clearFun();
        });

    }
})();
